// 生成腾讯云ocr授权签名
const cloud = require('wx-server-sdk')

var crypto = require('crypto');

cloud.init();


var secretId  = 'AKID50CjmjgAv6DazRd5fDr7wboKpWRZllM8',
    secretKey = 'eccxcDk6SeMzRo8J84qrlYSwQIICxb1J',
    appid     = '1258410954',
    pexpired  = 86400,
    userid   = 0;

var now = parseInt(Date.now() / 1000),
    rdm = parseInt(Math.random() * Math.pow(2, 32)),
    plainText = 'a=' + appid + '&k=' + secretId + '&e=' + (now+pexpired) + '&t=' + now + '&r=' + rdm + userid + '&f=',
      data = new Buffer(plainText,'utf8'),
    res = crypto.createHmac('sha1',secretKey).update(data).digest(),
    bin = Buffer.concat([res,data]);

var sign = bin.toString('base64');


// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  return {
    sign,
    openid: wxContext.OPENID
  }
}